import 'package:znmm/network/dio_https.dart';

import '../model/news_model.dart';

class NewsMag {
  static final NewsMag _singleton = NewsMag._internal();
  factory NewsMag() {
    return _singleton;
  }

  NewsMag._internal();

  void doSomething() {
    print("Doing something...");
  }

  //......
  late final List<NewsModel> _newList = [];
  //List<NewsModel> getNewList() => _newList;

  List<NewsModel> getNewList() {
    _newList.clear();
    // 模拟快讯数据
    _newList.add(NewsModel(
      title: 'Flutter 2.8 发布',
      imptType: 1,
      occurrenceTime: 'Flutter 2.8 版本推出，带来了许多新特性和改进。',
    ));

    _newList.add(NewsModel(
      title: 'Dart 2.15 发布',
      iType: 1,
      imptType: 1,
      occurrenceTime:
          'Dart 2.15 版本发布，增强了语言和工具的功能。Dart 2.15 版本发布，增强了语言和工具的功能。Dart 2.15 版本发布，增强了语言和工具的功能。Dart 2.15 版本发布，增强了语言和工具的功能。Dart 2.15 版本发布，增强了语言和工具的功能。Dart 2.15 版本发布，增强了语言和工具的功能。',
    ));

    return _newList;
  }

  //...request
  void reqFlashNews({void Function(dynamic)? callback}) {
    if (_newList.isNotEmpty) return;
    DioHttps dioUtil = DioHttps.instance!;
    //dioUtil.openLog();
    dioUtil
        .request<dynamic>(
      "https://www.quicknavig.com/nav-base-api/open/flashNews/getList",
      method: DioMethod.get,
    )
        .then((response) {
      // 处理响应数据

      //print("response:$response");
      if (response == null) return;

      var nodesList = response['datas']["records"] as List;
      print("nodesList:${nodesList.length}");

      _newList.clear();
      for (int iIndex = 0; iIndex < nodesList.length; iIndex++) {
        NewsModel pModel = NewsModel.fromJson(nodesList[iIndex]);
        _newList.add(pModel);
      }
      print("_newList:${_newList.length}");

      if (callback != null) {
        callback(_newList.length);
      }
    });
  }
}
